//
//  WBHomeViewController.swift
//  WB
//
//  Created by macssss on 16/6/29.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit

//原创cellId
private let originalCllId = "originalCllId"
//被转发cellId
private let retweetedCllId = "retweetedCllId"


class WBHomeViewController: WBBaseController {

    /// 列表视图模型
    private lazy var listViewModel = WBDataListViewModel()

  

    //MARK: - 加载表格数据
   override func loadData() {
    
//    print("准备刷新，最后一条 \(self.listViewModel.statusList.last?.text)")
        print("准备刷新表格")
        
        refreshControl?.beginRefreshing()
    
        //模拟加载加载
        DispatchQueue.main.after(when: DispatchTime.now() + 2) { 
            
            self.listViewModel.loadStatus(pullup: self.isPullup, completion: { (isSuccess, shouldRefresh) in
                
                self.refreshControl?.endRefreshing()
                
                //恢复上拉刷新
                self.isPullup = false
                
                if shouldRefresh {
                    
                    self.tabelView?.reloadData()
                }
            })
            
        }
    
    }


    
    //MARK:实现按钮方法
    @objc private func showFriends() {
        
        let vc = WBDome()
        
        //隐藏bar按钮
//        vc.hidesBottomBarWh enPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
}

//MARK: 设置界面
extension WBHomeViewController {
    
    
    //重写父类方法
    override func setupTabelView() {
        
        super.setupTabelView()
      //  设置导航栏
              navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        
//                tabelView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        tabelView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCllId)
        tabelView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCllId)
        
        //设置预估行高 和自动行高
//        tabelView?.rowHeight = UITableViewAutomaticDimension
        tabelView?.estimatedRowHeight = 300
        
        //取消分割线
        tabelView?.separatorStyle = .none
        
        setupNavTitle()
    }
    
    private func setupNavTitle() {
        

        
        let title = WBNetworkManager.shared.userAccount.screen_name
        let button = WBTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        
        //设置选中状态 不选中
        btn.isSelected = !btn.isSelected
    }
    
}


//MARK: 重写父类数据方法
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
         //取到模型
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellId = (vm.status.retweeted_status != nil) ? retweetedCllId : originalCllId
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
        
        //模型设置cell的数据
       cell.viewModel = vm
        
        
        return cell
    }
    //父类必须实现代理方法，子类菜能重写 只有重写此方法 才能把计算好的行高设置给cell
   override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        //获取index视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        // 返回计算好的行高
        return vm.rowHeight
        
    }
}






